package com.itheima.canal.listener;

import com.alibaba.otter.canal.protocol.CanalEntry;
import com.itheima.canal.conf.RabbitMqConfig;
import com.xpand.starter.canal.annotation.CanalEventListener;
import com.xpand.starter.canal.annotation.ListenPoint;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @author ZJ
 */
@CanalEventListener //声明当前的类是canal的监听类
public class BusinessListener {

    //获取 Spring容器中 的 RabbitTemplate
    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     *
     * @param eventType   连接数据库的类型
     * @param rowData   数据库发生改变的数据 (数据改变之前和改变之后都可以获取到)
     */
    @ListenPoint(schema = "changgou_business", table = {"tb_ad"})//设置监听的 数据库 和 表
    //如果监听的 数据库中的表发生改变 则 执行以下方法
    public void adUpdate(CanalEntry.EventType eventType, CanalEntry.RowData rowData) {
        System.err.println("广告数据发生变化");
        //获取 数据发生之后的 position 列的数据
        List<? extends CanalEntry.ColumnOrBuilder> afterColumnsOrBuilderList1 = rowData.getAfterColumnsOrBuilderList();
        for (CanalEntry.ColumnOrBuilder columnOrBuilder : afterColumnsOrBuilderList1) {
            if(columnOrBuilder.getName().equals("position")){
                String value = columnOrBuilder.getValue();
                //发送到 RabbitMq 队列中
                rabbitTemplate.convertAndSend("", RabbitMqConfig.AD_UPDATE_QUEQU,value);
            }
        }















        //测试代码 -------------------------------------------------

/*        List<? extends CanalEntry.ColumnOrBuilder> beforeColumnsOrBuilderList = rowData.getBeforeColumnsOrBuilderList();//获取数据库修改前的数据

        for (CanalEntry.ColumnOrBuilder columnOrBuilder : beforeColumnsOrBuilderList) {
            System.out.println("数据库发生改变之前数据 -- > " + "列名: " + columnOrBuilder.getName() + "列数据 : " + columnOrBuilder.getValue());
        }

        List<? extends CanalEntry.ColumnOrBuilder> afterColumnsOrBuilderList = rowData.getAfterColumnsOrBuilderList();

        for (CanalEntry.ColumnOrBuilder columnOrBuilder : afterColumnsOrBuilderList) {
            System.out.println("数据库发生改变之后数据 -- > " + "列名: " + columnOrBuilder.getName() + "列数据 : " + columnOrBuilder.getValue());
        }*/
        //测试代码 -------------------------------------------------
















//        //修改前数据
//        for(CanalEntry.Column column: rowData.getBeforeColumnsList()) {
//            if(column.getName().equals("position")){
//                System.out.println("发送消息到mq  ad_update_queue:"+column.getValue());
//                rabbitTemplate.convertAndSend("","ad_update_queue",column.getValue());  //发送消息到mq
//                break;
//            }
//        }
//
//        //修改后数据
//        for(CanalEntry.Column column: rowData.getAfterColumnsList()) {
//            if(column.getName().equals("position")){
//                System.out.println("发送消息到mq  ad_update_queue:"+column.getValue());
//                rabbitTemplate.convertAndSend("","ad_update_queue",column.getValue());  //发送消息到mq
//                break;
//            }
//        }
   }
    }
